

Component({
  properties: {
    // 是否开始展示游戏
    visible: {
      type: Boolean,
      value: false
    },
    countdown_extension: {
      type: Object,
      value: {}
    },
    countdown_time: {
      type: Number,
      value: 0
    },
  },
  data: {
    countdown:null,
    time:'',
    clock: '',
    total_micro_second : ''
  },
  ready: function () {
    clearTimeout(this.data.countdown)
    this.data.total_micro_second = Number( this.data.countdown_time) * 1000
    this.count_down();
  },
  detached: function () {
    this.data.countdown && clearTimeout(this.data.countdown)
  },
  methods: {
   
   
     /* 毫秒级倒计时 */
     count_down:function() {
       let _this = this
        // 渲染倒计时时钟
        _this.setData({
          clock: _this.date_format(_this.data.total_micro_second)
        });
        
        if (_this.data.total_micro_second <= 0) {
            _this.handleClose()
            return;
        }
        _this.data.countdown = setTimeout(function () {
        // 放在最后--
        _this.data.total_micro_second -= 10;
          _this.count_down();
        }, 10)
      },
       // 关闭
    handleClose: function () {
      this.triggerEvent("finish")
    },
      // 位数不足补零
      fill_zero_prefix:function(num) {
        return num < 10 ? "0" + num : num
      },
      

      // 时间格式化输出，如03:25:19 86。每10ms都会调用一次
      date_format:function(micro_second) {
        // 秒数
        var second = Math.floor(micro_second / 1000);
        // 小时位
        var hr = Math.floor(second / 3600);
        // 分钟位
        var min = this.fill_zero_prefix(Math.floor((second - hr * 3600) / 60));
        // 秒位
        var sec = this.fill_zero_prefix((second - hr * 3600 - min * 60));// equal to => var sec = second % 60;
        
        return min + ":" + sec;
      },
       // 开始准备倒计时
    cultdown: function () {
      let _this = this
      console.log('倒计时数据对象',_this.data.countdown_extension)
      _this.data.time = Number( _this.data.countdown_extension.countdown)
      _this.data.countdown=setInterval(function () {
        if(--_this.data.time <= 0 ){
          clearInterval(_this.data.countdown)
          // 关闭倒计时
          _this.handleClose()
        }
        _this.setData({
          time:_this.data.time
        })
      }, 1000)
    },
  }
});